//
//  HYFStatusRetweetView.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/24.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit
import SnapKit

//转发微博视图
class HYFStatusRetweetView: UIView {
    
    //记录当前转发微博的底部
    var retweetViewBottomConstraint:Constraint?
    
    
    //供外界赋值
    var statusViewModel:HYFStatusViewModel?{
        
        didSet{
            
            contentLabel.attributedText = statusViewModel?.retweetContentAttr
            
            //先卸载底部的约束
            retweetViewBottomConstraint?.uninstall()
            
            //代表有配图
            if statusViewModel?.status?.retweeted_status?.picture_urls?.count > 0 {
                
               //赋值
                pictureView.pictureInfo = statusViewModel?.status?.retweeted_status?.picture_urls
                
                //更改约束
                self.snp_makeConstraints(closure: { (make) in
                    
                    self.retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(cellMargin).constraint
                    
                })
                
                //显示配图
                pictureView.hidden = false
            }else{
                
                //更改约束
                self.snp_makeConstraints(closure: { (make) in
                    
                    self.retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(cellMargin).constraint
                })
                //隐藏配图
                 pictureView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        
        backgroundColor = RGB(237, green: 237, blue: 237)
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置contentLabel 约束
        contentLabel.snp_makeConstraints { (make) in
            
            make.top.leading.equalTo(cellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            //size 约束已经在 之前添加了
            
            make.top.equalTo(contentLabel.snp_bottom).offset(cellMargin)
            make.leading.equalTo((SCREENW - (cellWH*3 + cellMargin*2))/2)
        }
        
        //设置当前 view 的约束
        self.snp_makeConstraints { (make) in
            
            self.retweetViewBottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(cellMargin).constraint
        }
    }
    
    //懒加载控件
    private lazy var contentLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15, maxWidth: SCREENW - 20)
    
    //懒加载配图 属性
    private lazy var pictureView:HYFStatusPictureView = HYFStatusPictureView()
    
}
